/**
 * ship-device edit
 */
let _this;
let _loading;
// new Vue({
//     el: '#appContainVue',
//     data: {
//         size: '',
//         deviceStatus:'',
//         isTimeGroup: false,
//         loginU: {},
//         viewData: {},
//         levelArray: [],
//         levelData: [],
//         levelAllData: []
//     },
//     mounted() {
//         _this = this;
//         let u = localStorage.getItem("ADMIN_LOGIN");
//         let loginU = decryptLocalStorage(u);
//         this.loginU = loginU;
//         let _data = localStorage.getItem("workCardData");
//         if (_data != undefined) {
//             let viewData = decryptLocalStorage(_data, loginU.token);
//             console.log(viewData);
//             if (viewData.workSystem == '2') {
//                 this.isTimeGroup = true;
//             }
//         }
//     },
//     methods: {
//
//     }
// });

layui.use(['form', 'tableSelect', 'layedit', 'laydate'], function () {
    let form,layer,tableSelect,layedit,laydate,$;
    form = layui.form,
        layer = layui.layer,
        tableSelect = layui.tableSelect,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.$;

    let u = localStorage.getItem("ADMIN_LOGIN");
    let loginU = {};
    if (u == undefined) {
        layer.msg('请登录', {icon: 1, time: 500}, function () {
            window.location = 'page/login.html';
        });
    } else {
        loginU = decryptLocalStorage(u);
    }
    // if(loginU.shipId!=0){
    //     shipId=loginU.shipId
    // }
    let _data = localStorage.getItem("productData");
    let viewData = undefined;

    if (_data != undefined) {
        viewData = decryptLocalStorage(_data, loginU.token);
        $("#shipIds").val(viewData.shipIds);
        $("#shipNames").val(viewData.shipNames);
        $("#productName").val(viewData.productName);
        $("#productQuantity").val(viewData.productQuantity);
        $("#productPrice").val(viewData.productPrice);
        $("#sellFlag").val(viewData.sellFlag);
        $("#productDescribe").val(viewData.productDescribe);

        $("#receiveData").val(viewData.receiveData);
        $("#transmitData").val(viewData.transmitData);
        $("#quotaCycle").val(viewData.quotaCycle);
        $("#sessionLifetime").val(viewData.sessionLifetime);
        $("#accountLifetime").val(viewData.accountLifetime);
        $("#sonicwallId").val(viewData.sonicwallId);
        $("#sonicwallName").val(viewData.sonicwallName);

        $("#remark").val(viewData.remark);
        form.render('select');
    }
    loadShip();
    loadSonicwall();
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var params = data.field;
        params.token = loginU.token;
        params.productType='1';
        if(params.sessionLifetime>30 || params.sessionLifetime<1)
        {
            layer.msg('会话时间（天）范围在1天~30天', {
                icon: 2,
                time: 2000
            });
            return  false;
        }

        if(params.accountLifetime>365 || params.accountLifetime<1)
        {
            layer.msg('账号有效期（天）范围在1天~365天', {
                icon: 2,
                time: 2000
            });
            return  false;
        }

        if(params.sessionLifetime>=params.accountLifetime)
        {
            layer.msg('账号有效期需要大于会话时间', {
                icon: 2,
                time: 2000
            });
            return  false;
        }

        //添加
        if (viewData == undefined) {
            request.add('product', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg(result.msg || '保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        //修改
        else {
            params.productId = viewData.productId;
            request.update('product', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg(result.msg || '保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }

        return false;
    });


    //ship
    function loadShip() {
        let tableUrl = "/admin/v1/ship/page?shipStatus=营运&token=" + loginU.token;
        tableSelect.render({
            elem: '#shipNames',
            checkedKey: 'id',
            searchFlag: true,
            searchKey: 'shipName',
            where: {
                keyWork: JSON.stringify({shipId: 0})
            },
            table: {
                url: tableUrl,loginUserType:loginU.userType,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'shipId', title: 'ID'},
                    {field: 'shipCompanyName', title: '船舶公司'},
                    {field: 'shipName', title: '船舶'}
                ]]
            },
            done: function (elem, data) {
                if (data.data.length > 0) {
                    let shipIds = $("#shipIds").val();
                    let shipNames = $("#shipNames").val();
                    let shipIdArray = [];
                    let shipNameArray = [];
                    if (shipIds != '' && shipNames != '') {
                        shipIdArray = shipIds.split(",");
                        shipNameArray = shipNames.split(",");
                    }
                    layui.each(data.data, function (index, ship) {
                        let shipId = ship.shipId;
                        let shipName = ship.shipName;
                        shipIdArray.push(shipId);
                        shipNameArray.push(shipName);
                    });
                    $("#shipIds").val(shipIdArray.join(","));
                    $("#shipNames").val(shipNameArray.join(","));
                }
            },
            clear: function () {
                $("#shipIds").val("");
                $("#shipNames").val("");
            }
        })
    }


    function loadSonicwall() {
        let tableUrl = "/admin/v1/sonicwall/page?token=" + loginU.token;
        tableSelect.render({
            elem: '#sonicwallName',
            checkedKey: 'id',
            searchFlag: true,
            searchKey: 'sonicwallName',
            table: {
                url: tableUrl,loginUserType:loginU.userType,
                cols: [[
                    {type: 'radio'},
                    {field: 'sonicwallId', title: 'ID'},
                    {field: 'sonicwallName', title: 'sonicwall名称'}
                ]]
            },
            done: function (elem, data) {
                if (data.data.length > 0) {
                    $("#sonicwallId").val(data.data[0].sonicwallId);
                    $("#sonicwallName").val(data.data[0].sonicwallName);
                } else {
                    $("#sonicwallId").val("");
                    $("#sonicwallName").val("");
                }

            },
            clear: function () {
                $("#sonicwallId").val("");
                $("#sonicwallName").val("");

            }
        })
    }


});
